package com.ihg.hiex.entity.session.meeting;

import java.beans.Transient;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.session.Session;

/**
 * 会议
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "session_meeting")
public class SessionMeeting {
    
    @Id
    @GeneratedValue
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Session session;
    
    /** 会议名称 */
    private String name;
    
    /**
     * 开始时间，精确到分钟
     * 
     *  2018-02-12 13:30
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date startTime;
    
    /** 结束时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date endTime;
    
    /** 会议主持人 */
    private Long owner;
    
    /** 会议室 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private ConferenceRoom conferenceRoom;
    
    /** 参与者类型 */
    @Enumerated(EnumType.STRING)
    private Target target;
    
    /** 会议类型 */
    @Enumerated(EnumType.STRING)
    private Type type;
    
    /** 若会议类型为assessor-delegate delegateId */
    private Long delegate;
    
    /** 所属组别 */
    private String subgroup;
    
    /** 创建时间 */
    private Date createTime;
    
    @Column(name= "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public SessionMeeting() {
    }
    
    /**
     * 
     * @param session
     * @param template
     * @param owner
     * @param conferenceRoom
     * @param delegateId
     * @param sessionDate
     */
    public SessionMeeting(Session session, SessionMeetingTemplate template, Long owner, ConferenceRoom conferenceRoom, Long delegateId, Date sessionDate) {
        this.session = session;
        this.name = template.getName();
        this.startTime = template.getStartDate(sessionDate);
        this.endTime = template.getEndDate(sessionDate);
        this.owner = owner;
        this.conferenceRoom = conferenceRoom;
        this.target = template.getTarget();
        this.type = template.getType();
        this.delegate = delegateId;
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * 
     * @param session
     * @param name
     * @param startTime
     * @param endTime
     * @param owner
     * @param conferenceRoom
     * @param target
     * @param type
     */
    public SessionMeeting(Session session, String name, Date startTime, Date endTime, Long owner, ConferenceRoom conferenceRoom, Target target, Type type) {
        this.session = session;
        this.name = name;
        this.startTime = startTime;
        this.endTime = endTime;
        this.owner = owner;
        this.conferenceRoom = conferenceRoom;
        this.target = target;
        this.type = type;
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * @param session
     * @param template
     * @param conferenceRoom
     */
    public SessionMeeting(Session session, SessionMeetingTemplate template, ConferenceRoom conferenceRoom) {
        this.session = session;
        this.name = template.getName();
        this.startTime = template.getStartDate(session.getStartDate());
        this.endTime = template.getEndDate(session.getStartDate());
        if (template.getTarget().isAssessorOwner()) {
            this.owner = session.getAssessorOwner().getId();
        } else if (template.getTarget().isAdminOwner()) {
            this.owner = session.getSessionHost().getId();
        } else {
            this.owner = null;
        }
        this.conferenceRoom = conferenceRoom;
        this.target = template.getTarget();
        this.type = template.getType();
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * @param session
     * @param template
     * @param conferenceRoom
     * @param subgroup
     */
    public SessionMeeting(Session session, SessionMeetingTemplate template, ConferenceRoom conferenceRoom, String subgroup) {
        this.session = session;
        this.name = template.getName();
        this.startTime = template.getStartDate(session.getStartDate());     // FIXME
        this.endTime = template.getEndDate(session.getStartDate());
        if (template.getTarget().isAssessorOwner()) {
            this.owner = session.getAssessorOwner().getId();
        } else if (template.getTarget().isAdminOwner()) {
            this.owner = session.getSessionHost().getId();
        } else {
            this.owner = null;
        }
        this.conferenceRoom = conferenceRoom;
        this.target = template.getTarget();
        this.type = template.getType();
        this.createTime = new Date();
        this.subgroup = subgroup;
        this.disabled = false;
    }
    
    /**
     * 
     * @param owner
     * @param conferenceRoom
     * @return
     */
    public SessionMeeting edit(Long owner, ConferenceRoom conferenceRoom) {
        this.owner = owner;
        this.conferenceRoom = conferenceRoom;
        return this;
    }
    
    /**
     * @param conferenceRoom
     * @return
     */
    public SessionMeeting edit(ConferenceRoom conferenceRoom) {
        this.conferenceRoom = conferenceRoom;
        return this;
    }
    
    /**
     * @param owner
     * @param startTime
     * @param endTime
     * @return
     */
    public SessionMeeting edit(Long owner, String startTime, String endTime) {
        this.owner = owner;
        this.startTime = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
        this.endTime = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
        return this;    
    }
    
    /**
     * @param owner
     * @param conferenceRoom
     * @param startTime
     * @param endTime
     * @return
     */
    public SessionMeeting edit(Long owner, ConferenceRoom conferenceRoom, String startTime, String endTime) {
        this.owner = owner;
        this.conferenceRoom = conferenceRoom;
        this.startTime = DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm");
        this.endTime = DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm");
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isStartMeeting() {
        Date currentDate = new Date();
        Date beforeTime = DateUtils.toDate(DateFormatUtils.format(this.startTime, "yyyy-MM-dd"));
        return currentDate.after(beforeTime);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return
     */
    public Date getStartTime() {
        return startTime;
    }
    
    /**
     * @return
     */
    public Date getEndTime() {
        return endTime;
    }

    /**
     * @return the owner
     */
    public Long getOwner() {
        return owner;
    }

    /**
     * @return the conferenceRoom
     */
    public ConferenceRoom getConferenceRoom() {
        return conferenceRoom;
    }

    /**
     * @return the target
     */
    public Target getTarget() {
        return target;
    }

    /**
     * @return the type
     */
    public Type getType() {
        return type;
    }

    /**
     * @return the delegate
     */
    public Long getDelegate() {
        return delegate;
    }

    /**
     * @return the subgroup
     */
    public String getSubgroup() {
        return subgroup;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param startTime
     */
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    
    /**
     * @param endTime
     */
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(Long owner) {
        this.owner = owner;
    }

    /**
     * @param conferenceRoom the conferenceRoom to set
     */
    public void setConferenceRoom(ConferenceRoom conferenceRoom) {
        this.conferenceRoom = conferenceRoom;
    }

    /**
     * @param target the target to set
     */
    public void setTarget(Target target) {
        this.target = target;
    }

    /**
     * @param type the type to set
     */
    public void setType(Type type) {
        this.type = type;
    }

    /**
     * @param delegate the delegate to set
     */
    public void setDelegate(Long delegate) {
        this.delegate = delegate;
    }

    /**
     * @param subgroup the subgroup to set
     */
    public void setSubgroup(String subgroup) {
        this.subgroup = subgroup;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
